package com.yincai.carefreewater.base;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import com.yincai.carefreewater.tool.OkHttpTool;
import com.yincai.carefreewater.tool.PermissionUtils;

import java.lang.reflect.Method;

/**
 * Created by $zhangke on 2017/5/15.
 */

public class BaseActivity extends AppCompatActivity {
    public static final String TAG ="tag" ;
   public OkHttpTool okTool = OkHttpTool.getInstance();
    private View decorView;

//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        //获取顶层视图
//        decorView = getWindow().getDecorView();
//    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        //状态栏全透明
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        //状态栏全透明

        super.onCreate(savedInstanceState, persistentState);
        okTool = OkHttpTool.getInstance();

    }

    @Override
    protected void onStart() {
     //  init();
        super.onStart();
      //  callPhone(null);
       // showCamera(null);
       PermissionUtils.requestMultiPermissions(BaseActivity.this,mPermissionGrant);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void back(View view) {
        finish();
    }


    public void showCamera(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CAMERA, mPermissionGrant);
    }

    public void getAccounts(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_GET_ACCOUNTS, mPermissionGrant);
    }
    public void callPhone(View view) {

        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant);
    }

    public void readPhoneState(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_PHONE_STATE, mPermissionGrant);
    }

    public void accessFineLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_FINE_LOCATION, mPermissionGrant);
    }

    public void accessCoarseLocation(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_COARSE_LOCATION, mPermissionGrant);
    }

    public void readExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, mPermissionGrant);
    }

    public void writeExternalStorage(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE, mPermissionGrant);
    }

    public void recordAudio(View view) {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_RECORD_AUDIO, mPermissionGrant);
    }

    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
//                case PermissionUtils.CODE_RECORD_AUDIO:
//                   // ToastTool.toast("Result Permission Grant CODE_RECORD_AUDIO");
//                    break;
//                case PermissionUtils.CODE_GET_ACCOUNTS:
//                  //  ToastTool.toast("Result Permission Grant CODE_GET_ACCOUNTS");
//                    break;
//                case PermissionUtils.CODE_READ_PHONE_STATE:
//                   // ToastTool.toast("Result Permission Grant CODE_READ_PHONE_STATE");
//                    break;
                case PermissionUtils.CODE_CALL_PHONE:
                   // ToastTool.toast("Result Permission Grant CODE_CALL_PHONE");
                    break;
                case PermissionUtils.CODE_CAMERA:
                   // ToastTool.toast("Result Permission Grant CODE_CAMERA");
                    break;
//                case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
//                   // ToastTool.toast("Result Permission Grant CODE_ACCESS_FINE_LOCATION");
//                    break;
//                case PermissionUtils.CODE_ACCESS_COARSE_LOCATION:
//                  //  ToastTool.toast("Result Permission Grant CODE_ACCESS_COARSE_LOCATION");
//                    break;
//                case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
//                  //  ToastTool.toast("Result Permission Grant CODE_READ_EXTERNAL_STORAGE");
//                    break;
//                case PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE:
//                    //ToastTool.toast("Result Permission Grant CODE_WRITE_EXTERNAL_STORAGE");
//                    break;
//                default:
//                    break;
            }
        }
    };

    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }

    private void init(){
        int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                //  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
               // | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
              //  | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide
                 | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                 | View.SYSTEM_UI_FLAG_VISIBLE;
        //判断当前版本在4.0以上并且存在虚拟按键，否则不做操作
        if (Build.VERSION.SDK_INT < 19 || !checkDeviceHasNavigationBar()) {
            //一定要判断是否存在按键，否则在没有按键的手机调用会影响别的功能。如之前没有考虑到，导致图传全屏变成小屏显示。
            return;
        } else {
            // 获取属性
            decorView.setSystemUiVisibility(flag);
        }

    }

    /**
     * 判断是否存在虚拟按键
     * @return
     */
    public boolean checkDeviceHasNavigationBar() {
        boolean hasNavigationBar = false;
        Resources rs = getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class<?> systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {

        }
        return hasNavigationBar;
    }


}
